package leetcode.editor.cn.q1_300.q50;

import java.util.Arrays;

/**
 * 抄的官方答案，自己的 “传染” 思路是错的，无法判断之前的数是否被传染
 * <p>
 * 使用第一行和第一列来标记本行、列是否需要被修改为 0
 *
 * @author qidi
 * @date 2021-12-15 21:03:16
 */
class Q0073_SetMatrixZeroes {
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public void setZeroes(int[][] matrix) {
            // 第一列是否需要全部变成 0
            boolean flag = false;

            int row = matrix.length;
            int col = matrix[0].length;

            for (int i = 0; i < row; i++) {
                if (matrix[i][0] == 0) {
                    flag = true;
                    break;
                }
            }

            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    if (matrix[i][j] == 0) {
                        matrix[i][0] = 0;
                        if (j != 0)
                            matrix[0][j] = 0;
                    }
                }
            }

            for (int i = 1; i < row; i++) {
                if (matrix[i][0] == 0) {
                    for (int j = 1; j < col; j++) {
                        matrix[i][j] = 0;
                    }
                }
            }

            for (int i = 1; i < col; i++) {
                if (matrix[0][i] == 0) {
                    for (int j = 1; j < row; j++) {
                        matrix[j][i] = 0;
                    }
                }
            }

            if (matrix[0][0] == 0) {
                for (int i = 0; i < col; i++) {
                    matrix[0][i] = 0;
                }
            }

            if (flag) {
                for (int i = 0; i < row; i++) {
                    matrix[i][0] = 0;
                }
            }
        }

    }
    //leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) {
        Solution solution = new Q0073_SetMatrixZeroes().new Solution();
        int[][] m;
        m = new int[][]{
                {1, 2, 3, 4},
                {5, 0, 7, 8},
                {0, 10, 11, 12},
                {13, 14, 15, 0}
        };
        solution.setZeroes(m);
        for (int[] ints : m) {
            System.out.println(Arrays.toString(ints));
        }

        m = new int[][]{
                {1, 1, 1}, {1, 0, 1}, {1, 1, 1}
        };
        solution.setZeroes(m);
        for (int[] ints : m) {
            System.out.println(Arrays.toString(ints));
        }

        m = new int[][]{
                {0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}
        };
        solution.setZeroes(m);
        for (int[] ints : m) {
            System.out.println(Arrays.toString(ints));
        }

        m = new int[][]{
                {1, 0, 1, 1, 0, 1},
                {1, 1, 1, 1, 1, 1},
                {1, 1, 1, 1, 1, 1},
                {1, 0, 1, 1, 0, 1},
                {1, 1, 1, 0, 1, 1},
                {1, 1, 1, 1, 0, 1},
                {1, 1, 1, 1, 1, 1},
        };
        solution.setZeroes(m);
        for (int[] ints : m) {
            System.out.println(Arrays.toString(ints));
        }

    }
}